package com.xoms.admin.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xoms.admin.controller.res.bean.RProStockBean;
import com.xoms.admin.controller.res.bean.RProStoreBean;
import com.xoms.admin.model.AccountBean;
import com.xoms.admin.model.ProStoreBean;
import com.xoms.admin.model.vo.ProStockVo;
import com.xoms.admin.persist.dao.ProStockDao;
import com.xoms.admin.persist.dao.ProStoreDao;
import com.xoms.admin.service.ProStockService;
import com.xoms.admin.service.exception.ServiceException;
import com.xoms.admin.service.res.PageResult;
import com.xoms.util.AppTextUtil;
import com.xoms.util.DateTimeUtil;
import com.xoms.util.StringUtil;

@Service
public class ProStockServiceImpl implements ProStockService {

	@Autowired
	ProStockDao proStockImpl;
	
	@Autowired
	ProStoreDao proStoreImpl;

	@Override
	public PageResult<RProStoreBean> getStoreHouses(AccountBean account, String key, int page, int pagesize, int type) {
		PageResult<RProStoreBean> result = new PageResult<RProStoreBean>();
		List<RProStoreBean> resList = new ArrayList<RProStoreBean>();
		// 查询柜台仓库数量
		int count = proStoreImpl.selectStoreCount(key, type);
		// 查询仓库柜台列表
		if (count > 0) {
			List<ProStoreBean> lists = proStoreImpl.selectStoreList(key, page, pagesize, type);
			if (lists != null) {
				RProStoreBean rb = null;
				for (ProStoreBean acc : lists) {
					rb = new RProStoreBean();
					rb = parse(acc);
					resList.add(rb);
				}
			}
		}
		result.setData(resList);
		result.setCount(count);
		return result;
	}

	@Override
	public boolean del(AccountBean account, String sid) {
		boolean flag = false;
		// 仓库内有商品则无法删除
		int count = proStockImpl.selectCount(sid, null);
		if(count > 0) {
			throw new ServiceException("store_is_used");
		}
		flag = proStoreImpl.del(sid) > 0;
		if (!flag) {
			throw new ServiceException("database_delete_fail", "柜台仓库");
		}
		return flag;
	}

	@Override
	public RProStoreBean editStoreHouse(AccountBean account, String sid, String name, String theatreId, String remark,
			Double limitMoney, int type, int status, Double totalCost, String adminId) {
		ProStoreBean data = new ProStoreBean();
		boolean flag = false;
		data.setName(name);
		data.setTheatreId(theatreId);
		data.setRemark(remark);
		data.setLimitMoney(limitMoney);
		data.setType(type);
		data.setStatus(status);
		data.setAdminId(adminId);
		data.setTotalCost(totalCost);
		
		if(StringUtil.isEmpty(sid)) {
			sid = AppTextUtil.getPrimaryKey();// sid
			data.setSid(sid);
			flag = proStoreImpl.insert(data) > 0;
			if (!flag) {
				throw new ServiceException("database_insert_fail", "柜台仓库");
			}
		} else {
			data.setSid(sid);
			flag = proStoreImpl.update(data) > 0;
			if (!flag) {
				throw new ServiceException("database_update_fail", "柜台仓库");
			}
		}
		
		return parse(proStoreImpl.selectOneById(sid));
	}

	/**
	 * 格式化数据
	 * 
	 * @param punit
	 * @return
	 */
	private RProStoreBean parse(ProStoreBean acc) {
		RProStoreBean res = new RProStoreBean();
		res.setSid(acc.getSid());
		res.setRemark(acc.getRemark());
		res.setTheatre(acc.getTheatre());
		res.setName(acc.getName());
		res.setCreateTime(DateTimeUtil.formatTimestamp(acc.getCreateTime()));
		res.setLimitMoney(acc.getLimitMoney());
		res.setAccount(acc.getAccount());
		res.setAdmin(acc.getAdmin());
		res.setTotalCost(acc.getTotalCost());
		res.setType(acc.getType());
		res.setStatus(acc.getStatus());
		return res;
	}

	@Override
	public PageResult<RProStockBean> getProStocks(AccountBean account, String key, int page, int pagesize,
			String storeHouseId) {
		PageResult<RProStockBean> result = new PageResult<RProStockBean>();
		List<RProStockBean> resList = new ArrayList<RProStockBean>();
		// 查询商品仓库列表
		int count = proStockImpl.selectCount(storeHouseId, key);
		if (count > 0) {
			List<ProStockVo> lists = proStockImpl.selectStocks(key, page, pagesize, storeHouseId);

			if (lists != null) {
				RProStockBean rb = null;
				for (ProStockVo acc : lists) {
					rb = new RProStockBean();
					rb.setAccount(acc.getAccount());
					rb.setProductId(acc.getProductId());
					rb.setStoreHouseId(acc.getStoreHouseId());
					rb.setStoreHouse(acc.getStoreHouse());
					rb.setCost(acc.getCost());
					rb.setStore(acc.getStore());
					rb.setProduct(acc.getProduct());
					rb.setProductNo(acc.getProductNo());
					rb.setSpecs(acc.getSpecs());
					rb.setProductBar(acc.getProductBar());
					rb.setCategory(acc.getCategory());
					rb.setUnit(acc.getUnit());
					rb.setCreateTime(DateTimeUtil.formatTimestamp(acc.getCreateTime()));
					resList.add(rb);
				}
			}
		}
		result.setData(resList);
		result.setCount(count);
		return result;
	}

}
